/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newspaper;

import entity.Image;
import entity.News;
import entity.Tag;
import java.io.IOException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jms.JMSException;

import model.ImageModel;
import model.NewsModel;
import model.TagModel;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import service.ActiveMQService;
import util.SocketContentHandle;
import util.TagHelper;

/**
 * 
 * @author xuanhung2401
 */
public class VnexpressContent extends Thread {

	static Document doc = null;
	static NewsModel newsModel = null;
	private static ImageModel imgModel = new ImageModel();
	String url = "";

	public VnexpressContent(String urlString) {
		url = urlString;
	}

	@SuppressWarnings("deprecation")
	public void process() {
		newsModel = new NewsModel();
		News n = new News();
		n.setUrl(url);
		// News n = newsModel.findByUrl(this.url);
		try {
			doc = SocketContentHandle.getSiteContentBySocket("vnexpress.net",
					n.getUrl());
		} catch (UnknownHostException ex) {
			Logger.getLogger(VnexpressContent.class.getName()).log(
					Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(VnexpressContent.class.getName()).log(
					Level.SEVERE, null, ex);
		}
		Elements content = doc.select("div[style=overflow:hidden]");
		if (content.size() > 0) {

			// loai bo cac trang flash.
			if (content.select("#flashContent").size() > 0) {
				newsModel.delete(n);
				return;
			}

			// loai bo cac trang co qua nhieu link video.
			Elements videoLink = content.select("a[href*=.asp]");
			if (videoLink.size() > 3) {
				newsModel.delete(n);
				return;
			}

			// lay cac url tin chi tiet trong trang.
//			VnexpressURL urlThread = new VnexpressURL("vnexpress.net",
//					n.getUrl());
//			VnexpressThreadManager.listURLThread.add(urlThread);

			// lay thong tin category va subcategory, alias.
			String url = n.getUrl();
			System.out.println(url);
			url = url.replaceAll("http://", "");
			String[] arrayUrl = url.split("/");
			if (arrayUrl.length == 6) {
				n.setCategory(arrayUrl[2]);
				n.setSubCategory(arrayUrl[2]);
				n.setAlias(arrayUrl[5]);
			}
			if (arrayUrl.length == 7) {
				n.setCategory(arrayUrl[2]);
				n.setSubCategory(arrayUrl[3]);
				n.setAlias(arrayUrl[6]);
			}

			System.out.println(n.getAlias());
			System.out.println(n.getCategory());
			System.out.println(n.getSubCategory());

			// lay title.
			String title = content.select(".Title").text();
			n.setTitle(title);
			content.select(".Title").remove();

			// lay description va link lien quan.
			Elements description = content.select("h2[class=Lead]");
			content.select("h2[class=Lead]").remove();
			Elements relateUrls = description.select("a");
			ArrayList<String> arrayRelate = new ArrayList<String>();
			for (Element relateUrl : relateUrls) {
				String relate = "http://vnexpress.net" + relateUrl.attr("href");
				arrayRelate.add(relate);
			}
			n.setRelateUrls(arrayRelate);
			System.out.println("This is relate link =========");
			for (String string : arrayRelate) {
				System.out.println(string);
			}
			description.select("a").remove();
			n.setDescription(description.text().replaceAll(">", ""));
			System.out.println(n.getDescription());

			// lay tag va date.
			n.setTagged(addTag());
			n.setDoc(addDate());
			Calendar cal = Calendar.getInstance();
			n.setDog(cal.getTime());
			content.select("script").remove();

			// lay image.
			Elements listImg = content.select("img");
			int temp = 0;
			for (Element e : listImg) {
				String imgLink = e.attr("src").toString();
				String[] sp = imgLink.split("/");
				String imgName = sp[sp.length - 1];
				imgName = (n.getAlias() + "_" + imgName).toLowerCase();
				Image img = new Image();
				img.setUrl("http://vnexpress.net/" + imgLink); 
				img.setName(imgName);
				img.setYear("" + cal.get(Calendar.YEAR));
				img.setMonth("" + cal.get(Calendar.MONTH) + 1);
				img.setDay("" + cal.get(Calendar.DATE));
				img.setStatus(1);
				imgModel.save(img);
				try {
					ActiveMQService.sendImageSMS(img.getUrl());
					System.out.println("send sms ok !");
				} catch (JMSException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				String regex = "img[src=" + imgLink + "]";
				try {
					String newImageSrc = "http://localhost:8080/"
							+ "images_news/" + img.getYear() + "/"
							+ img.getMonth() + "/" + img.getDay() + "/"
							+ imgName;
					content.select(regex).attr("src", newImageSrc);
					if (temp == 0) {
						n.setImgDescription(newImageSrc);
						temp = 1;
					}
				} catch (Exception exception) {
					continue;
				}
			}

			// lay author
			n.setAuthor(content.select("p[align=right]").text());
			content.select("p[align=right]").remove();
			// content.select("table[width!=1]").remove();//remove video.

			n.setComplete(true);
			n.setContent(content.html().replaceAll("<a[^>]+>", "")
					.replaceAll("<\\/[ ]*a>", ""));
			n.setStatus(1);
			newsModel.save(n);
		} else {
			newsModel.delete(n);
			System.out.println("Outside link !");
		}
	}

	public static ArrayList<String> addTag() {
		TagModel tagModel = new TagModel();
		// your tag from your site
		System.out.println("Get Tag ===========");
		Elements tags = doc.select(".tag-text");
		Elements yourTag = tags.select("a");
		ArrayList<String> listTag = new ArrayList<String>();
		for (Element e : yourTag) {
			String[] split = e.attr("href").split("/");
			String alias = split[split.length - 1];
			Tag hadTag = tagModel.findByAlias(alias);
			if (hadTag != null && !hadTag.getAlias().equalsIgnoreCase("")) {
				hadTag.setCount(hadTag.getCount() + 1);
				tagModel.save(hadTag);
			} else {
				Tag newTag = new Tag();
				newTag.setAlias(alias);
				newTag.setCount(1);
				newTag.setTitle(e.text());
				tagModel.save(newTag);
			}
			if (!listTag.contains(alias)) {
				listTag.add(alias);
			}
		}

		System.out.println("Get my tag =======");
		// my Tag born from o2tv
		Document myDoc = TagHelper.help(doc);
		Elements myTags = myDoc.select(".tag");
		if (myTags.size() > 0) {
			for (int i = 0; i < myTags.size(); i++) {
				String alias = myTags.get(i).text().toLowerCase();
				Tag hadTag = tagModel.findByAlias(alias);
				if (hadTag != null && !hadTag.getAlias().equalsIgnoreCase("")) {
					hadTag.setCount(hadTag.getCount() + 1);
					tagModel.save(hadTag);
				} else {
					Tag newTag = new Tag();
					newTag.setAlias(alias);
					newTag.setCount(1);
					newTag.setTitle(alias);
					tagModel.save(newTag);
				}
				if (!listTag.contains(alias) && !alias.equalsIgnoreCase("")
						&& alias != null) {
					listTag.add(alias);
					System.out.println(alias);
				}
			}
		}
		System.out.println("Finish get tag");
		return listTag;
	}

	public static Date addDate() {
		System.out.println("Get date=====");
		try {
			Elements folderHeader = doc.select(".folder-header");
			String d = folderHeader.select(".fr").text();
			String[] split = d.split(",");
			System.out.println(split[1]);
			String d1 = split[1].replaceAll(" ", "");
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
			Date convertedDate = dateFormat.parse(d1.toString());
			System.out.println(convertedDate);
			return convertedDate;
		} catch (ParseException ex) {
			Logger.getLogger(VnexpressContent.class.getName()).log(
					Level.SEVERE, null, ex);
			return new Date();
		}
	}

	public void run() {
		process();
	}
}
